﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Core.Interface;
using Service.Core.Result;
using Tools;
using Web.Filter;
using Web.Models.AnalysisData;
using Web.Service;
using Web.ViewModel.AnalysisData;
using Web.ViewModel.CoreData;

namespace Web.Controllers
{

    [UserPermissionsFilter]
    public class HomeController : BaseController
    {
        private ReportService reportService = ReportService.GetInstance();
        private ReportAnalysisService reportAnalysisService = ReportAnalysisService.GetInstance();
        private ShipService shipService = ShipService.GetInstance();
        private CompanyService companyService = CompanyService.GetInstance();
        private UserService userService = UserService.GetInstance();


        /// <summary>
        /// 用户入口
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public ActionResult Init(String account)
        {
            if (String.IsNullOrEmpty(account))
            {
                return RedirectError();
            }
            else
            {
                var userCookie = new HttpCookie("user", account);
                userCookie.Expires.AddDays(365);
                Response.Cookies.Add(userCookie);
                string companyId = userService.GetUser(account).Company;
                if(!string.IsNullOrEmpty(companyId))
                {
                    Session["UserInfo"] = companyService.QueryCompanyModel(companyId);
                }
            }
            return RedirectToAction("Index", "Home");
        }

       

        #region 报文


        /// <summary>
        /// 报文列表页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ReportList(DateTime? start, DateTime? end,String mmsi,String type)
        {
            long startTime = DateTimeHelper.ConvertDateTimeInt(DateTime.Now.AddDays(-3));
            long endTime = DateTimeHelper.ConvertDateTimeInt(DateTime.Now);
            if (start.HasValue && end.HasValue)
            {
                startTime = DateTimeHelper.ConvertDateTimeInt(start.Value);
                endTime = DateTimeHelper.ConvertDateTimeInt(end.Value.AddDays(1));
            }
            ViewData["Ships"] = shipService.GetUserShipsModels(Request.Cookies["user"]?.Value).OrderBy(m=>m.Name).ToList();
            return View(reportService.GetReportList(startTime, endTime, Request.Cookies["user"]?.Value, mmsi, type));
        }

        /// <summary>
        /// 统计报文页面
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public ActionResult CountReport(DateTime? start, DateTime? end)
        {
            long startTime = DateTimeHelper.ConvertDateTimeInt(DateTime.Now.AddDays(-1));
            long endTime = DateTimeHelper.ConvertDateTimeInt(DateTime.Now);
            if (start.HasValue && end.HasValue)
            {
                startTime = DateTimeHelper.ConvertDateTimeInt(start.Value);
                endTime = DateTimeHelper.ConvertDateTimeInt(end.Value.AddDays(1));
            }
            return View(reportService.CountReport(startTime, endTime, Request.Cookies["user"]?.Value));
        }

        #endregion

        #region 报文统计分析

        /// <summary>
        /// 报文统计列表页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            if(Request.Cookies["user"]==null)
            {
                return RedirectError();
            }
            if (Session[Request.Cookies["user"].Value] == null)
            {
                IServiceResult result = reportAnalysisService.ReadReportAnalysis(Request.Cookies["user"]?.Value);
                if ((ResultType)result.GetCode() == ResultType.Success)
                {
                    ViewData["Data"] = result.GetData();
                    Session[Request.Cookies["user"].Value] = (List<ReportAnalysis>)ViewData["Data"];
                }
            }
            else
            {
                ViewData["Data"] = Session[Request.Cookies["user"].Value];
            }
            return View();
        }

        /// <summary>
        /// 报文统计分析页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ReportAnalysisDetail(String id)
        {
            if (String.IsNullOrEmpty(id) || Session["UserMMSI"] == null)
            {
                return RedirectError();
            }
            string mmsi = Session["UserMMSI"].ToString();
            ReportAnalysis model = ((List<ReportAnalysis>)Session[Request.Cookies["user"].Value]).Where(m => m.Id == id && mmsi.Contains(m.MMSI)).FirstOrDefault();
            if (model == null)
            {
                return RedirectError();
            }
            ViewData["Data"] = JsonHelper.DeserializeObject(model.AnalyseData, typeof(ReportAnalysisModel));
            return View();
        }

        #endregion


    }
}